﻿using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using System.Web;
namespace Artech.PetShop.Common
{
    public class ApplicationContext:Dictionary<string, object>
    {
        public const string ContextKey = "Artech.PetShop.Infrastructures.ApplicationContext";
        public const string ContextHeaderLocalName = "ApplicationContext";
        public const string ContextHeaderNamespace = "http://www.artech.com/petshop/";
        public static ApplicationContext Current
        {
            get
            {
                if (HttpContext.Current != null)
                {
                    if (HttpContext.Current.Session[ContextKey] == null)
                    {
                        HttpContext.Current.Session[ContextKey] = new ApplicationContext();
                    }

                    return HttpContext.Current.Session[ContextKey] as ApplicationContext;
                }

                if (CallContext.GetData(ContextKey) == null)
                {
                    CallContext.SetData(ContextKey, new ApplicationContext());
                }

                return CallContext.GetData(ContextKey) as ApplicationContext;
            }
           set
            {
                if (HttpContext.Current != null)
                {
                    HttpContext.Current.Session[ContextKey] = value; ;
                }
                else
                {
                    CallContext.SetData(ContextKey, value);
                }
            }
        }
        public string UserName
        {
            get
            {
                if (!this.ContainsKey("__UserName" ))
                {
                    return string.Empty;
                }

                return (string)this["__UserName"];
            }
            set
            {
                this["__UserName"] = value;
            }
        }
    }
}
